<?php
/**
 * SocialEngine
 *
 * @category   Application_Core
 * @package    Translator
 * @copyright  Copyright 2008-2011 NewroSoftMods and TaurusSoft
 * @license    http://www.socialengine.net/license/
 * @author     Bezugliy Sergey
 */
class Translator_Installer extends Engine_Package_Installer_Module
{
    public function onPreInstall()
    {
        set_time_limit(0);
        
        // Get form
        $form = new Engine_Form();
        
        $form->addAttribs(array(
            'class' => 'global_form'
        ));
        
        $form->addElement('Text', 'key', array(
            'label' => 'License Key',
            'required' => true,
            'allowEmpty' => false,
        ));
        
        $form->addElement('Button', 'submit', array(
            'type' => 'submit',
            'label' => 'Submit'
        ));
        
        $styles = '<style type="text/css"> ' .
            '.package_query_results ul { border: none; width: auto; } ' .
            '.package_query_results ul li { padding: 0; font-size: 10px; } ' .
            'div.admin_packages_install_error { display: none; } ' .
            'form ul.form-errors { width: 210px; }' .
            '</style>';
        
        $request = Zend_Controller_Front::getInstance()->getRequest();
        
        if (!$request->isPost()) {
            $this->_error($styles . $form->render());
            return false;
        }
        
        if (!$form->isValid($request->getPost())) {
            $this->_error($styles . $form->render());
            return false;
        }
        
        // Process...
        include_once('license/license.php');
        
        if (IIIIIIIIIIII($this, $form->getValue('key'))) {
            $this->clearErrors();
        } else {
            $form->getElement('key')->addError('The license key you have entered is invalid!
            Please enter a valid license key and try again.');
            $this->_error($styles . $form->render());
            return false;
        }
        
        
        parent::onPreInstall();
    }
    
    public function onInstall()
    {
        parent::onInstall();
        
        // Correct queries succeeded message
        $messages = $this->getMessages();
        $sessinoCountQuery = new Zend_Session_Namespace('CountQuery');
        $count = $sessinoCountQuery->count;
        $this->clearMessages();
        
        if (!empty($messages) and $count) {
            foreach ($messages as $message) {
                if (strstr($message, 'queries succeeded')) {
                    $this->_message(sprintf('%1$d queries succeeded.', $count));
                } else {
                    $this->_message($message);
                }
            }
        }
    }
}
